package com.br4mmie.asciistory.characters;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.*;

import com.br4mmie.asciistory.Restorable;
import com.br4mmie.asciistory.battle.EncounterAction;
import com.br4mmie.asciistory.battle.EncounterPlayer;
import com.br4mmie.asciistory.mission.Armor;
import com.br4mmie.asciistory.mission.Inventory;
import com.br4mmie.asciistory.mission.InventoryItem;
import com.br4mmie.asciistory.mission.Item;
import com.br4mmie.asciistory.mission.Mission;
import com.br4mmie.asciistory.mission.Weapon;


public class Player implements Restorable
{
	private Inventory inventory;
	private ArrayList<Mission> missions;
	
	//battle info
	public ArrayList<EncounterAction> skills;
	public int totalDamage;
	public int baseDamage = 6;
	public int weaponDamage = 0;
	
	//stats
	public int maxHP = 100;
	public int currentHP = maxHP;
	
	public int totalDefence;
	public int baseDefence = 5;
	public int armorDefence = 0;
	
	public int exp = 0;
	public int expTNL = 100;
	public int level = 1;
	
	//equipment
	//private Weapon weapon = new Weapon(null, null, 0, null, 0, null);
	
	public Player()
	{
		inventory = new Inventory(10);
		missions = new ArrayList<Mission>();
		skills = new ArrayList<EncounterAction>();
		totalDamage = baseDamage + weaponDamage;
		totalDefence = baseDefence + armorDefence;
		
		//VOOR TESTEN LATER VERWIJDEREN!!!!
		skills.add(new EncounterAction("Data/Other/StickAttack.action"));
		//skills.add(new EncounterAction("Data/Other/SlimeAttack.action"));
	}
	
	public int getWeaponDamage()
	{
		//int tempDamage = 0;
		//int d = 0;
		//Weapon w = null;
		for (InventoryItem e: inventory.equipment)
		{	
			Item i = e.getItem();
			if (i instanceof Weapon)
			{
				Weapon w = (Weapon) i;
				weaponDamage = w.getDamage();
			}
		}
		totalDamage = baseDamage + weaponDamage;
		return totalDamage;
	}
	
	public int getArmorDefence()
	{
		//Armor a = null;
		armorDefence = 0;
		for ( InventoryItem i : inventory.equipment )
		{
			Item ii = i.getItem();
			if (ii instanceof Armor)
			{
				Armor a = (Armor) ii;
				armorDefence += a.getDefence();
			}
		}
		totalDefence = baseDefence + armorDefence;
		return totalDefence;
	}
	
	public Inventory getInventory()
	{
		return inventory;
	}
	
	public ArrayList<Mission> getMissions()
	{
		return missions;
	}
	
	public EncounterPlayer getEncounterPlayer()
	{
		return new EncounterPlayer(this);
	}
	
	public void gainExp(int xp)
	{
		exp += xp;
		if (exp >= expTNL)
		{
			level++;
			exp -= expTNL;

			float nxpTNL = 0.0f;
			for (int i=0; i<level+1; i++)
			{
				int add = (int)(i + 300.0 * Math.pow(2, i/7.0));
				nxpTNL += add;
			}
			
			expTNL = (int)nxpTNL;
			
			baseDamage = (int)(baseDamage * 1.2f);
			baseDefence = (int)(baseDefence * 1.2f);
			maxHP = (int)(maxHP * 1.5f);
			currentHP = maxHP;
		}
	}

	public ArrayList<EncounterAction> getSkills() 
	{
		return skills;
	}
	
	public void restoreMission(Mission mission)
	{
		missions.add(mission);
	}
	public void restoreInventory(Inventory inv)
	{
		inventory = inv;
	}
	
	private static int getNumber(String line)
	{
		return Integer.parseInt(line.split(" ")[1]);
	}
	public static Player restore(BufferedReader reader)
	{
		try
		{
			Player player = new Player();
			player.maxHP = 			getNumber(reader.readLine());
			player.currentHP = 		getNumber(reader.readLine());
			
			player.totalDefence = 	getNumber(reader.readLine());
			player.baseDefence = 	getNumber(reader.readLine());
			player.armorDefence = 	getNumber(reader.readLine());
			
			player.totalDamage =	getNumber(reader.readLine());
			player.baseDamage = 	getNumber(reader.readLine());
			player.weaponDamage = 	getNumber(reader.readLine());
			
			player.exp = 			getNumber(reader.readLine());
			player.expTNL =			getNumber(reader.readLine());
			player.level = 			getNumber(reader.readLine());
			reader.readLine();
			return player;
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public void save(BufferedWriter writer) 
	{
		try 
		{
			writer.write("player\n");
			writer.write("maxHP "+maxHP+"\n");
			writer.write("currentHP "+currentHP+"\n");
			
			writer.write("totalDefence "+totalDefence+"\n");
			writer.write("baseDefence "+baseDefence+"\n");
			writer.write("armorDefence "+armorDefence+"\n");
			
			writer.write("damage "+totalDamage+"\n");
			writer.write("baseDamage "+baseDamage+"\n");
			writer.write("weaponDamage "+weaponDamage+"\n");
			
			writer.write("exp "+exp+"\n");
			writer.write("expTNL "+expTNL+"\n");
			writer.write("level "+level+"\n");
			writer.write("end\n");
		} catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}
